package com.platform.modules.app.controller;

import com.alibaba.fastjson.JSONObject;
import com.platform.common.exception.BusinessException;
import com.platform.common.utils.Constant;
import com.platform.common.utils.JwtUtils;
import com.platform.common.utils.RestResponse;
import com.platform.common.validator.AbstractAssert;
import com.platform.modules.custmoer.dto.LoginDtoParams;
import com.platform.modules.custmoer.entity.StaffEntity;
import com.platform.modules.custmoer.service.StaffService;
import com.platform.modules.sys.entity.MallUserEntity;
import com.platform.modules.sys.entity.SysConfigEntity;
import com.platform.modules.sys.service.SysConfigService;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

@Slf4j
@RestController
@RequestMapping("/app")
public class AppLoginController {

    @Autowired
    private JwtUtils jwtUtils;
    @Autowired
    private StaffService staffService;
    @Autowired
    private SysConfigService sysConfigService;

    /**
     * 用户名密码登录
     *
     * @return RestResponse
     */
    //@IgnoreAuth
    @PostMapping("loginByMobile")
    public RestResponse loginByMobile(@RequestBody JSONObject params) {
        String mobile = params.getString("mobile");
        AbstractAssert.isBlank(mobile, "手机号不能为空");

        String password = params.getString("password");
        AbstractAssert.isBlank(password, "密码不能为空");
        //用户登录
        //MallUserEntity user = userService.loginByMobile(mobile, password);

        Map<String,Object> para = new HashMap<>();
        para.put("phone",mobile);
        List<StaffEntity> list = staffService.queryAll(para);
        if(null == list || list.size() == 0){
            throw new BusinessException("用户不存在");
        }

        StaffEntity staffEntity = list.get(0);

        if(!mobile.equals(staffEntity.getPhone())){
            throw new BusinessException("账号不正确");
        }
        if(!password.equals(staffEntity.getPassword())){
            throw new BusinessException("密码不正确");
        }

        //根据员工账号id生成token--staffId
        String token = jwtUtils.generateToken(staffEntity.getId());


        Map<String, Object> map = new HashMap<>(4);
        map.put("token", token);
        map.put("staffId", staffEntity.getId());// 员工表id
        //map.put("openId", user.getOpenId());
        //map.put("user", user);
        map.put("expire", Constant.EXPIRE);

        return RestResponse.success(map);
    }

    @GetMapping("/parsetoken/{token}")
    public RestResponse parseToken(@PathVariable String token){
        Claims claim = jwtUtils.getClaimByToken(token);
        Map<String, Object> map = new HashMap<>();
        map.put("subject",claim.getSubject());
        return RestResponse.success(map);
    }

    /**
     * 退出系统
     * 暂时没有好的办法，客户端先控制
     * @return RestResponse
     */
    @GetMapping("/logout/{token}")
    public RestResponse getToken(@PathVariable String token) {

        return RestResponse.success();
    }

    @GetMapping("/isUpdate")
    public RestResponse getVersion() {
        SysConfigEntity configEntity = sysConfigService.getById("3");
        return RestResponse.success().put("config",configEntity);
    }

}
